package org.hpin.assetImport.web;

import java.io.File;
import java.text.ParseException;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hpin.asset.entity.Assetinfo;
import org.hpin.assetImport.service.AssetImportService;
import org.hpin.common.core.SpringTool;
import org.hpin.common.core.web.BaseAction;
import org.hpin.common.util.HttpTool;
import org.hpin.common.widget.pagination.Page;
@Namespace("/assetImport")
@Action("assetImportAction")
@Results({
	@Result(name = "importAssetImport", location = "/WEB-INF/assetImport/importAssetImport.jsp")
})
public class AssetImportAction extends BaseAction {
	AssetImportService service = (AssetImportService)SpringTool.getBean(AssetImportService.class);
	Assetinfo assetinfo;
	private File affix;
    private String affixContentType;
    private String affixFileName;
	
	/**
	 * 资产临时表导入
	 * @return
	 */
	public String saveAssetImport() {
		JSONObject json = new JSONObject();
		try {
			//保存
			int count = 0;
			//时间
			System.out.println(affixFileName);
			String type = affixFileName.substring(affixFileName.lastIndexOf(".")+1, affixFileName.length());
			if ("xlsx".equals(type)) {
				count = service.saveAssetImport(affix, affixFileName);
	    		json.accumulate("statusCode", 200);
	    		if(count>=0){
	    			json.accumulate("message", "您成功导入"+count+"条数据！");
	    		}else if(count ==-1){
	    			json.accumulate("message", "请核对您的表头是否正确！");
	    		}
	    		json.accumulate("navTabId", "menu_40288581404d3df301404d4ef3650003");
	    		json.accumulate("callbackType", "1,"+count);
	    		
			}else if (!"xlsx".equals(type)) {
				json.accumulate("statusCode", 300);
	    		json.accumulate("message", "导入异常,请检查EXCEL");
	    		json.accumulate("navTabId", "menu_31");
	    		json.accumulate("callbackType", "1,0");
			}
			
		}
		catch (Exception ex) {
			ex.printStackTrace();
    		json.accumulate("statusCode", 300);
    		json.accumulate("message", "导入异常,请检查EXCEL");
    		json.accumulate("navTabId", "menu_31");
    		json.accumulate("callbackType", "1,0");
		}finally{
			renderJson(json);
		}
		return null;
	} 
	
	
	
	public String importAssetImport(){
		return "importAssetImport";
	}
	
	
	public Assetinfo getAssetinfo() {
		return assetinfo;
	}


	public void setAssetinfo(Assetinfo assetinfo) {
		this.assetinfo = assetinfo;
	}


	public File getAffix() {
		return affix;
	}


	public void setAffix(File affix) {
		this.affix = affix;
	}


	public String getAffixContentType() {
		return affixContentType;
	}


	public void setAffixContentType(String affixContentType) {
		this.affixContentType = affixContentType;
	}


	public String getAffixFileName() {
		return affixFileName;
	}


	public void setAffixFileName(String affixFileName) {
		this.affixFileName = affixFileName;
	}


}
